package com.pioneer.controller.client;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.pioneer.common.annotation.Log;
import com.pioneer.common.core.controller.BaseController;
import com.pioneer.common.core.domain.AjaxResult;
import com.pioneer.common.enums.BusinessType;
import com.pioneer.common.utils.SecurityUtils;
import com.pioneer.web.client.domain.CliAddress;
import com.pioneer.web.client.service.ICliAddressService;
import jakarta.annotation.Resource;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;

/**
 * 通知地址
 *
 * @author hlm
 */
@RestController
@RequestMapping("/client/address")
public class CliAdressController extends BaseController {

    @Resource
    private ICliAddressService addressService;

    /**
     * 获取通知地址列表
     *
     * @param address 查询条件
     * @return 结果
     */
    @PreAuthorize("@ss.hasPerm('client:address:list')")
    @GetMapping("/list")
    public AjaxResult list(CliAddress address) {
        boolean isPage = startPage();
        CliAddress address1 = address;
        address1.setClientId(SecurityUtils.getUserId());
        List<CliAddress> list = addressService.list(Wrappers.query(address1));
        return success(isPage, list);
    }

    /**
     * 根据通知地址id获取详细信息
     *
     * @param addressId 通知地址id
     * @return 结果
     */
    @PreAuthorize("@ss.hasPerm('client:address:query')")
    @GetMapping(value = "/{addressId}")
    public AjaxResult getInfo(@PathVariable Long addressId) {
        return AjaxResult.success(addressService.getById(addressId));
    }

    /**
     * 新增通知地址
     *
     * @param address 通知地址
     * @return 结果
     */
    @PreAuthorize("@ss.hasPerm('client:address:add')")
    @Log(title = "通知地址", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@Validated @RequestBody CliAddress address) {
        address.setClientId(SecurityUtils.getUserId());
        return toAjax(addressService.save(address));
    }

    /**
     * 修改通知地址
     *
     * @param address 通知地址
     * @return 结果
     */
    @PreAuthorize("@ss.hasPerm('client:address:edit')")
    @Log(title = "通知地址", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@Validated @RequestBody CliAddress address) {
        return toAjax(addressService.updateById(address));
    }

    /**
     * 删除通知地址
     *
     * @param addressIds 通知地址id数组
     * @return 结果
     */
    @PreAuthorize("@ss.hasPerm('client:address:remove')")
    @Log(title = "通知地址", businessType = BusinessType.DELETE)
    @DeleteMapping("/{addressIds}")
    public AjaxResult remove(@PathVariable Long[] addressIds) {
        return toAjax(addressService.removeByIds(Arrays.asList(addressIds)));
    }
}
